// pages/search/search.js
let http = require('../../utils/api.js');
import {
    myHttps
} from '../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        iconsize: 12,
        searchList: '',
        nosearch: '', //控制没有搜索历史页面
        // searchContent: '', //清空历史搜索的数据
        clear: false,//取消叉号
        val: '',//输入框的值
        userId: '',//用户id

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            userId: wx.getStorageSync('userId')
        })
        this.getSearch()
    },

    //  历史搜索接口
    getSearch() {
        let url = http.api.history + this.data.userId + '/searches';
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                if (res.data.content.length == 0) {
                    this.setData({
                        nosearch: 2
                    })
                } else {
                    this.setData({
                        searchList: res.data.content,
                        nosearch: 1
                    })
                }
            }
        }).catch(err => { })
    },
    // 点击清空 走接口
    clearnSearch() {
        let url = http.api.clearnSearch + this.data.userId + '/searches';
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.getSearch()
            }
        }).catch(err => { })
    },
    // 删除单个搜索记录 点击小叉叉 走接口
    clearnOne(e) {
        let index = e.currentTarget.dataset.index
        let id = e.currentTarget.dataset.id
        let val = e.currentTarget.dataset.content
        // console.log(val)
        let url = http.api.delSearchOne + this.data.userId + '/search?keyword=' + val;
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.getSearch()
            }
        }).catch(err => {})
    },
    //点击回车请求
    keywordSearch(e) {
        var val = e.detail.value
        // console.log(e)
        //判断输入的值不能为空
        if (val == '' || val == undefined) {
            wx.showToast({
                title: '主题或资讯名称不能为空',
                icon: 'none',
                duration: 3000
            })
        } else if (val) {
            wx.navigateTo({
                url: '../searchFinish/searchFinish?_q=' + val,
            })
        }

    },
    // 监控输入框的改变事件 点输入框后面的小叉叉 清空输入框内容
    bindKeyInput(e) {
        let val = e.detail.value
        this.setData({
            val: val
        })
        if (val == '') {
            this.setData({
                clear: false
            })
        } else {
            this.setData({
                clear: true
            })
        }
    },
    // 点击输入框后面的小叉叉  清空输入框的内容
    clearContent() {
        console.log(this.data.val)
        // console.log(100)
        this.setData({
            val: ''
        })
    },
    // 点击每一条 走搜索 走搜索接口
    contentSearchs(e) {
        var _q = e.currentTarget.dataset.content
        // console.log(_q)
        wx.navigateTo({
            url: '../searchFinish/searchFinish?_q=' + _q,
        })

    },
})